package cn.leelei.mybatis.test.entity;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import cn.leelei.mybatis.entity.UUIDT;
import cn.leelei.mybatis.entity.User;

public class UUIDTest2 {

	@Test
	public void Savetest() throws IOException {
		// 加载配置文件
			InputStream is = Resources.getResourceAsStream("mybatis.xml");
			SqlSessionFactoryBuilder Builder = new SqlSessionFactoryBuilder();
		
			SqlSessionFactory sqlSessionFactory = Builder.build(is);
			// 创建SQLsession //默认事物位false
			SqlSession session = sqlSessionFactory.openSession();
			
			// 执行操作 sql语句 第一个参数 sql语句的id namescope+id
			UUIDT uuid = new UUIDT();
			uuid .setUser_name("lg");
			uuid .setCnname("李规");
			uuid .setSex(0);
			uuid .setMobile("13800138000");
			uuid .setNote("爱学习的李规");
			uuid .setEmail("123@169.com");
			session.insert("UUIDT.save", uuid );
			// 提交事务
			System.err.println(uuid.getId().length());
			session.commit();
			session.close();
	}
	/**
	 * 查询方法一
	 * @throws IOException
	 */
	@Test
	public void Querytest() throws IOException {
		// 加载配置文件
			InputStream is = Resources.getResourceAsStream("mybatis.xml");
			SqlSessionFactoryBuilder Builder = new SqlSessionFactoryBuilder();
		
			SqlSessionFactory sqlSessionFactory = Builder.build(is);
			
			SqlSession session = sqlSessionFactory.openSession();
			User user = session.selectOne("test.query",1);
			
			session.close();
		/*	System.err.println(user.getUser_name());*/
	}
	/**
	 * 查询方法二
	 * @throws IOException
	 */
	@Test
	public void Query1test() throws IOException {
		// 加载配置文件
			InputStream is = Resources.getResourceAsStream("mybatis.xml");
			SqlSessionFactoryBuilder Builder = new SqlSessionFactoryBuilder();
		
			SqlSessionFactory sqlSessionFactory = Builder.build(is);
			
			SqlSession session = sqlSessionFactory.openSession();
			List<User> user = session.selectOne("test.fuzzy","z%");
			
			session.close();
			System.err.println(user.size());
	}

}
